package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;
    //专门处理声音的
    @Autowired
    private VodService vodService;




    /**
     * 批量获取下单付费声音列表
     *
     * @param trackId
     * @param trackCount
     * @return
     */
    @Operation(summary = "批量获取下单付费声音列表")
    @GetMapping("/findPaidTrackInfoList/{trackId}/{trackCount}")
    Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable("trackId") Long trackId, @PathVariable("trackCount") Integer trackCount) {
        List<TrackInfo> trackInfoList = trackInfoService.findPaidTrackInfoList(trackId, trackCount);
        return Result.ok(trackInfoList);
    }

//    @TsLogin
    @Operation(summary = "获取用户声音分集购买支付列表")
    @GetMapping("/findUserTrackPaidList/{trackId}")
    public Result findUserTrackPaidList(@PathVariable Long trackId) {
//        Long userId = AuthContextHolder.getUserId();
        List<Map<String, Object>> map = trackInfoService.findUserPaidTrackList(trackId);
        return Result.ok(map);

    }

    @Operation(summary = "获取专辑对应的声音列表")
    @TsLogin(required = false)
    @GetMapping("/findAlbumTrackPage/{albumId}/{pageNo}/{pageSize}")
    public Result findAlbumTrackPage(@PathVariable Long albumId,
                                     @PathVariable Long pageNo,
                                     @PathVariable Long pageSize) {

        Long userId = AuthContextHolder.getUserId();
        Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(pageNo, pageSize);
        IPage<AlbumTrackListVo> albumTrackListVoIPage = trackInfoService.findAlbumTrackPage(albumTrackListVoPage, albumId, userId);
        return Result.ok(albumTrackListVoPage);
    }


    @TsLogin
    @Operation(summary = "修改声音")
    @PutMapping("/updateTrackInfo/{trackId}")
    public Result updateTrackInfo(@PathVariable Long trackId,
                                  @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(trackInfoVo, trackId);
        return Result.ok();

    }

    @Operation(summary = "根据声音Id获取声音对象")
    @GetMapping("getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable Long trackId) {
        TrackInfo trackInfo = trackInfoService.getById(trackId);
        return Result.ok(trackInfo);
    }

    /**
     * 删除音频
     *
     * @param trackId
     * @return
     */
    @Operation
    @DeleteMapping("/removeTrackInfo/{trackId}")
    public Result removeTrackInfo(@PathVariable Long trackId) {
        boolean saveOrUpdate = trackInfoService.removeTrackInfo(trackId);
        return Result.ok();
    }

    @TsLogin
    @Operation(summary = "查询声音列表")
    @PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
    public Result findUserTrackList(@PathVariable Long pageNo,
                                    @PathVariable Long pageSize,
                                    @RequestBody TrackInfoQuery trackInfoQuery) {
        //查询专辑列表的时候需要登录,查看自己的创建的列表
        Long userId = null == AuthContextHolder.getUserId() ? 1l : AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        Page<Object> trackListVoPage = new Page<>(pageNo, pageSize);

        IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(trackListVoPage, trackInfoQuery);
        return Result.ok(trackListVoIPage);
    }

    @TsLogin
    @Operation(summary = "保存声音")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        //调用服务层方法保存数据
        Long userId = null == AuthContextHolder.getUserId() ? 1l : AuthContextHolder.getUserId();
        trackInfoService.saveTrackInfo(trackInfoVo, userId);
        return Result.ok();

    }

    @Operation(summary = "上传声音")
    @PostMapping("/uploadTrack")
    public Result uploadTrack(MultipartFile file) {
        //	调用服务层方法; 包含fileId ，url
        Map<String, Object> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

}
